package com.credithc.signature.core.util;


import org.redisson.api.RBucket;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;


/***

 * Redis client的辅助工具类

 * 用于连接Redis服务器 创建不同的Redis Server对应的客户端对象

 */
@Component
public class RedisUtils {

    @Autowired
    private RedissonClient redisson;

    public String getString(String key) {
        RBucket<String> result = redisson.getBucket(key);
        return result.get();
    }

    public void setString(String key, Object value, long timeOut) {
        RBucket<Object> result = redisson.getBucket(key);
        result.set(value, timeOut, TimeUnit.MINUTES);
    }

    public void setString(String key, Object value) {
        RBucket<Object> result = redisson.getBucket(key);
        result.set(value);
    }

    public Boolean exists(String key) {
        RBucket<Object> result = redisson.getBucket(key);
        if (result.isExists()) {
            return true;
        }
        return false;
    }

    public RLock getLock(String name) {
        return redisson.getLock(name);
    }

}

